package com.tom.service.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import com.tom.util.HttpRequest;
import com.tom.util.JDBCUtil;

public class QianZuo {

	/**
	 * 
	 * @author hanpengda
	 * @date 2018年5月15日
	 * @TODO 获取钱座的投资信息
	 */
	public static void work(){
		try {
			
			ArrayList<String> urList = JDBCUtil.getUrls("qss_qianzuo_bussiness_data");
			ArrayList<String> list = new ArrayList<String>();
			for (int i = 0; i <urList.size(); i++) {
				String url = "http://www.iqianzuo.com/invest/investRecord?borrow_id=";
				String URL = urList.get(i);
				URL = URL.replaceAll("http://www.iqianzuo.com/invest/", "").replaceAll(".html", "");
				//System.out.println(URL);
				url += URL;
				//System.out.println(url);
				String result = HttpRequest.sendZrbYyGet(url);
				//System.out.println(result);
				Thread.sleep(1000);
				//System.out.println(result);
				String [] results = result.split("</td>");
				String tzxx = "";
				for (int j = 0; j < results.length; j++) {
					result = results[j];
					String regEx_html = "<[^>]+>";
					Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);  
				    Matcher m_html = p_html.matcher(result);
				    result = m_html.replaceAll("");
				    //System.out.println(result);
				    result = result.trim().replaceAll("￥", "");
				    
				    tzxx += result + "==";
				   
				}
				tzxx = tzxx.substring(0, tzxx.length()-6).replaceAll("====", "###");
				String sql = "update qss_qianzuo_bussiness_data set tzxx ="+"'"+tzxx+"'"+" where url = "+"'"+urList.get(i)+"'"+"";
				list.add(sql);
				//添加数据库操作
				//JDBCUtil.insertData(list);
				System.out.println(sql);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}